#ifndef __WIZ_INNER_WINDOWS_MUTEX_HPP__SHANHAOBO_19800429__
#define __WIZ_INNER_WINDOWS_MUTEX_HPP__SHANHAOBO_19800429__

#include <Windows.h>

#include "../WizInnerMutex.hpp"

namespace Wiz
{
    namespace Inner
    {
        namespace Mutex
        {
            namespace Windows
            {
                class Mutex_t : public ::Wiz::Inner::Mutex::Mutex_t
                {
                protected:
                    HANDLE m_Mutex;
                public:
                    Mutex_t()
                    {
                        m_Mutex = ::CreateMutex(0, 0, 0);
                        WIZ_ASSERT(m_Mutex != NULL);
                    }
                    ~Mutex_t()
                    {
                        ::CloseHandle(m_Mutex);
                    }
                public:
                    virtual ::Wiz::Bool::Type TimeAcquire(::Wiz::UInt32::In t)
                    {
                        DWORD r = ::WaitForSingleObject(m_Mutex, t);
                        if (r == WAIT_OBJECT_0)
                        {
                            return ::Wiz::Bool::True;
                        }
                        else if (r == WAIT_TIMEOUT)
                        {
                            return ::Wiz::Bool::False;
                        }

                        WIZ_ASSERT(::Wiz::Bool::False);
                        return ::Wiz::Bool::False;
                    }
                    virtual ::Wiz::Void::Type Acquire()
                    {
                        ::Wiz::Bool::Type r = this->TimeAcquire(INFINITE);
                        WIZ_ASSERT(r);
                    }
                    virtual ::Wiz::Bool::Type TryAcquire()
                    {
                        return this->TimeAcquire(0);
                    }
                    virtual ::Wiz::Void::Type Release()
                    {
                        DWORD r = ::ReleaseMutex(m_Mutex);
                        WIZ_ASSERT(r != 0);
                    }
                };
            } /// end of namespace windows
        } /// end of namespace Mutex
    } /// end of namespace Inner
} /// end of namespace Wiz

#endif /*__WIZ_INNER_WINDOWS_MUTEX_HPP__SHANHAOBO_19800429__*/
